//
//  JASetupViewController.m
//  JAGatherAdDemo
//
//  Created by Yc on 2024/1/29.
//

#import "SetupViewController.h"
#import <JAGatherAdSDK/JAGatherAdSDK.h>
#import "SetupCell.h"
#import "Header.h"

@interface SetupViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataList;

@end

@implementation SetupViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupUI];
    [self setupData];
    [self.tableView reloadData];
}

- (void)setupData {
    NSString *idfa = [JAGDeviceInfoUtils deviceIDFA];
    NSString *idfv = [JAGDeviceInfoUtils deviceIDFV];
    NSString *sdkVersion = [JAGSDKConfig sdkVersion];
    NSString *appVersion = [NSString stringWithFormat:@"%@(%@)",[JAGAppInfoUtils appVersion],[JAGAppInfoUtils appBuildVersion]];
    NSString *appBundle = [JAGAppInfoUtils appBundleIdentifier];
    
    self.dataList = @[@{@"Title":@"广告个性化推荐",@"Type":@(SetupTypePersonalized),@"Describe":@""},
                      @{@"Title":@"开屏热启动",@"Type":@(SetupTypeSplashHotStart),@"Describe":@""},
                      @{@"Title":@"开屏显示自定义底部视图",@"Type":@(SetupTypeSplashCustomBottom),@"Describe":@""},
                      @{@"Title":@"IDFA",@"Type":@(SetupTypeIdfa),@"Describe":idfa},
                      @{@"Title":@"IDFV",@"Type":@(SetupTypeIdfv),@"Describe":idfv},
                      @{@"Title":@"SDK版本",@"Type":@(SetupTypeSdkVersion),@"Describe":sdkVersion},
                      @{@"Title":@"应用版本",@"Type":@(SetupTypeAppVersion),@"Describe":appVersion},
                      @{@"Title":@"应用包名",@"Type":@(SetupTypeAppBundle),@"Describe":appBundle},
    ].mutableCopy;
}

- (void)setupUI {
    [self.view addSubview:self.tableView];
}

- (void)viewWillLayoutSubviews {
    self.tableView.frame = self.view.bounds;
}

#pragma mark - 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SetupCell *cell = [tableView dequeueReusableCellWithIdentifier:@"JASetupSwitchCell"];
    if (!cell) {
        cell = [[SetupCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"JASetupSwitchCell"];
    }
    
    NSDictionary *data = self.dataList[indexPath.row];
    SetupType type = [data[@"Type"] integerValue];
    [cell setupType:type
              title:data[@"Title"]
           describe:data[@"Describe"]
        switchState:[self obtainSwitchState:type]];
    __weak typeof(self) ws = self;
    [cell setSwitchBlock:^(BOOL isOn, SetupCell * _Nonnull cell) {
        __strong typeof(ws) self = ws;
        [self setupConfigState:isOn type:cell.type];
    }];
    
    [cell setCopyBlock:^(NSString * _Nonnull content) {
        if (content.length > 0) {
            UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
            pasteboard.string = content;
            TOAST(@"复制成功");
        }
    }];

    return cell;
}

- (BOOL)obtainSwitchState:(SetupType)type {

    if (type == SetupTypePersonalized) {
        return [JAGSDKConfig getPersonalizedAdState];;
    } else if (type == SetupTypeSplashHotStart) {
        return [[NSUserDefaults standardUserDefaults] boolForKey:SplashHotStartkey];
    } else if (type == SetupTypeSplashCustomBottom) {
        return [[NSUserDefaults standardUserDefaults] boolForKey:SplashBottomViewkey];
    } else {
        return NO;
    }
}

- (void)setupConfigState:(BOOL)isOn type:(SetupType)type {
    
    if (type == SetupTypePersonalized) {
        [JAGSDKConfig setPersonalizedAdState:isOn];
    } else if (type == SetupTypeSplashHotStart) {
        [[NSUserDefaults standardUserDefaults] setBool:isOn forKey:SplashHotStartkey];
        [[NSUserDefaults standardUserDefaults] synchronize];
    } else if (type == SetupTypeSplashCustomBottom) {
        [[NSUserDefaults standardUserDefaults] setBool:isOn forKey:SplashBottomViewkey];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}


#pragma mark - Lazy
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.rowHeight = 70;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = [UIColor whiteColor];
        [_tableView registerClass:[SetupCell class] forCellReuseIdentifier:@"JASetupSwitchCell"];
    }
    return _tableView;
}

@end
